package com.cucker.urlforwarder.service;

import com.cucker.urlforwarder.config.MyconfigProperties;
import com.cucker.urlforwarder.entity.Record;
import com.cucker.urlforwarder.mapper.RecordMapper;
import com.cucker.urlforwarder.util.FqdnUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RecordService {
    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private MyconfigProperties myconfigProperties;

    public List<Record> getURLForwardRecordByHostAndZone(String host, String zone) {
        if (host == null || zone == null) {
            return null;
        }
        if (host.equals("") || zone.equals("")) {
            return null;
        }
        List<Record> records = recordMapper.getURLForwardRecord(host, zone, myconfigProperties.getResolutionLine());
        // 智能DNS解析。
        // 当前线路没有添加记录，则查询默认线路的记录
        if (records == null || records.size() == 0) {
            if (!(myconfigProperties.getResolutionLine().equals("all") || myconfigProperties.getResolutionLine().equals("0"))) {
                records = recordMapper.getURLForwardRecord(host, zone, "0");
            }
        }
        return records;
    }

    public List<Record> getURLForwardRecordByFqdn(String fqdn) {
        int deep = myconfigProperties.getFqdnSearchDeep();
        if (deep < 0) {
            return null;
        }

        Map<String, String> f = new HashMap<>(), ft = new HashMap<>();
        List<Record> records = new ArrayList<>();


        for (int i = 1; i <= deep; ++i) {
            // 1. 直接本域名。
            // 如 FQDN 为 @.sys.qq.com
            // 则 host 为 @，zone 为 sys.qq.com
            // 或 host 为 @.sys，zone 为 qq.com
            // ...
            f = FqdnUtil.fqdnSplit("@." + fqdn, i);
            if (f == null) {
                return null;
            }
            records = getURLForwardRecordByHostAndZone(f.get("host"), f.get("zone"));
            if (records.size() > 0) {
                return records;
            }

            // 2. 精确域名。
            // 如 FQDN 为 book.sys.qq.com
            // 则 host 为 book，zone 为 sys.qq.com
            // 或 host 为 book.sys，zone 为 qq.com
            // ...
            f = FqdnUtil.fqdnSplit(fqdn, i);
            if (f == null) {
                return null;
            }
            records = getURLForwardRecordByHostAndZone(f.get("host"), f.get("zone"));
            if (records.size() > 0) {
                return records;
            }

            // 3. 泛域名。
            // 如 FQDN 为 *.sys.qq.com，
            // 则 host 为 *，zone 为 sys.qq.com
            // 或 host 为 *.sys，zone 为 qq.com
            // ...
            ft = FqdnUtil.fqdnSplit(fqdn, i);
            if (ft == null || ft.size() == 0) {
                return null;
            }
            f = FqdnUtil.fqdnSplit("*." + ft.get("zone"), i);
            if (f == null) {
                return null;
            }
            records = getURLForwardRecordByHostAndZone(f.get("host"), f.get("zone"));
            if (records.size() > 0) {
                return records;
            }
        }

        return null;
    }
}
